package com.example.xunua_zhinengjiaotong;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.xunua_zhinengjiaotong.adapter.MyLogListViewAdapter;
import com.example.xunua_zhinengjiaotong.utils.ETCLogBean;
import com.example.xunua_zhinengjiaotong.utils.HttpUtil;
import com.example.xunua_zhinengjiaotong.utils.MyApp;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

public class StopCarMessageActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView img_back;
    private TextView tv_log;
    private ListView lv_log;
    private HttpUtil util=new HttpUtil();
    private MyApp myApp;
    private ArrayList<ETCLogBean> loglist;

    Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    //直接用ETC的javaBean和适配器就行了
                    ArrayList<ETCLogBean> mData= (ArrayList<ETCLogBean>) msg.obj;
                    lv_log.setAdapter(new MyLogListViewAdapter(mData,StopCarMessageActivity.this));
                    tv_log.setVisibility(View.GONE);
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stop_car_message);
        initView();
        getData();
    }

    private void getData() {
        loglist=new ArrayList<>();
        loglist.clear();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    JSONArray getPakingListReport = util.connectServer_JsonArray("{}", myApp, "GetPakingListReport");
                    for (int i = 0; i < getPakingListReport.length(); i++) {
                        String s = getPakingListReport.getString(i);
                        JSONObject jsonObject = new JSONObject(s);
                        String money = jsonObject.getString("Money");
                        String carId = jsonObject.getString("CarId");
                        String parkInTime = jsonObject.getString("ParkInTime");
                        String parkOutTime = jsonObject.getString("ParkOutTime");
                        ETCLogBean bean=new ETCLogBean(money,carId,parkInTime,parkOutTime);
                        loglist.add(bean);
                    }
                    if (loglist.size() > 0) {
                        Message message = new Message();
                        message.what=0;
                        message.obj=loglist;
                        handler.sendMessage(message);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void initView() {
        myApp= (MyApp) getApplication();
        img_back=findViewById(R.id.stopcarMessage_img_back);
        img_back.setOnClickListener(this);
        tv_log=findViewById(R.id.stop_tv_log);
        lv_log=findViewById(R.id.stop_lv_log);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.stopcarMessage_img_back:
                finish();
                break;
        }
    }
    @Override
    public void onBackPressed() {
        // super.onBackPressed();
    }
}
